package com.beiing.leafchartdemo.slice;


import com.beiing.leafchat.LeafLineChart;
import com.beiing.leafchat.LeafSquareChart;
import com.beiing.leafchat.bean.*;
import com.beiing.leafchartdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class LeafChartSlice extends AbilitySlice {

    LeafLineChart leafLineChart;

    LeafSquareChart leafSquareChart;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_activity_leaf_chart);
        leafLineChart = (LeafLineChart) findComponentById(ResourceTable.Id_leaf_chart);
        leafSquareChart = (LeafSquareChart) findComponentById(ResourceTable.Id_leaf_square_chart);
        //测试折线图
        initLineChart();
        //测试直方图
        initSquareChart();
        findComponentById(ResourceTable.Id_toChartInFragment).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                toChartInFragment();
            }
        });
        findComponentById(ResourceTable.Id_SlideSelectLineChart).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                 SlideSelectLineChart();
            }
        });
        findComponentById(ResourceTable.Id_OutsideLineChart).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                OutsideLineChart();
            }
        });
    }



    private void initSquareChart() {
        Axis axisX = new Axis(getAxisValuesX());
        axisX.setAxisColor(RgbPalette.parse("#FF4081")).setTextColor(Color.DKGRAY.getValue()).setHasLines(false);
        Axis axisY = new Axis(getAxisValuesY());
        axisY.setAxisColor(RgbPalette.parse("#FF4081")).setTextColor(Color.DKGRAY.getValue()).setHasLines(false).setShowText(true);
        leafSquareChart.setAxisX(axisX);
        leafSquareChart.setAxisY(axisY);
        leafSquareChart.setChartData(getSquares());
    }

    private void initLineChart() {
        Axis axisX = new Axis(getAxisValuesX());
        axisX.setAxisColor(RgbPalette.parse("#33B5E5")).setTextColor(Color.DKGRAY.getValue()).setHasLines(true);
        Axis axisY = new Axis(getAxisValuesY());
        axisY.setAxisColor(RgbPalette.parse("#33B5E5")).setTextColor(Color.DKGRAY.getValue()).setHasLines(true).setShowText(true);
        leafLineChart.setAxisX(axisX);
        leafLineChart.setAxisY(axisY);

        List<Line> lines = new ArrayList<>();
        lines.add(getFoldLine());
        leafLineChart.setChartData(lines);
//        leafLineChart.showWithAnimation(1000);
        leafLineChart.show();
    }

    private List<AxisValue> getAxisValuesX(){
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            AxisValue value = new AxisValue();
            value.setLabel(i + "月");
            axisValues.add(value);
        }
        return axisValues;
    }

    private List<AxisValue> getAxisValuesY(){
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 0; i < 11; i++) {
            AxisValue value = new AxisValue();
            value.setLabel(String.valueOf(i * 10));
            axisValues.add(value);
        }
        return axisValues;
    }

    private Line getFoldLine(){
        List<PointValue> pointValues = new ArrayList<>();

        PointValue p = new PointValue();
        p.setX(0);
        p.setLabel(String.valueOf(90));
        p.setY(90 / 100f);
        pointValues.add(p);
        for (int i = 2; i <= 12; i++) {
            PointValue pointValue = new PointValue();
            pointValue.setX( (i - 1) / 11f);
//            int var = (int) (Math.random() * 100);
            int var = 20;
            pointValue.setLabel(String.valueOf(var));
            pointValue.setY(var / 100f);
            pointValues.add(pointValue);
        }


        Line line = new Line(pointValues);
        line.setLineColor(RgbPalette.parse("#33B5E5"))
                .setLineWidth(3)
                .setPointColor(Color.YELLOW.getValue())
                .setCubic(true)
                .setPointRadius(3)
                .setFill(true)
                .setFillColor(RgbPalette.parse("#33B5E5"))
                .setHasLabels(true)
                .setLabelColor(RgbPalette.parse("#33B5E5"));
        return line;
    }

    private Square getSquares(){
        List<PointValue> pointValues = new ArrayList<>();
        SecureRandom random = new SecureRandom();
        for (int i = 1; i <= 12; i++) {
            PointValue pointValue = new PointValue();
            pointValue.setX( (i - 1) / 11f);
            int var = (int) (random.nextFloat() * 100);
            pointValue.setLabel(String.valueOf(var));
            pointValue.setY(var / 100f);
            pointValues.add(pointValue);
        }

        Square square = new Square(pointValues);
        square.setBorderColor(RgbPalette.parse("#FF4081"))
                .setWidth(20)
                .setFill(false)
                .setHasLabels(true)
                .setLabelColor(RgbPalette.parse("#FF4081"));
        return square;
    }

    public void toChartInFragment() {
        Intent secondIntent = new Intent();
        // 指定待启动FA的bundleName和abilityName
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(getBundleName()+".ChartAbility")
                .build();
        secondIntent.setOperation(operation);
        // 通过AbilitySlice的startAbility接口实现启动另一个页面
        startAbility(secondIntent);

    }

    public void SlideSelectLineChart() {
        Intent secondIntent = new Intent();
        // 指定待启动FA的bundleName和abilityName
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(getBundleName()+".SlideSelectLineChartAbility")
                .build();
        secondIntent.setOperation(operation);
        // 通过AbilitySlice的startAbility接口实现启动另一个页面
        startAbility(secondIntent);
    }

    public void OutsideLineChart() {
        Intent secondIntent = new Intent();
        // 指定待启动FA的bundleName和abilityName
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(getBundleName()+".OutsideLineChartAbility")
                .build();
        secondIntent.setOperation(operation);
        // 通过AbilitySlice的startAbility接口实现启动另一个页面
        startAbility(secondIntent);
    }
}
